package com.bsj.travel.mqtt.config;

import com.bsj.travel.cached.redis.RedisCached;
import com.bsj.travel.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.travel.mqtt.cluster.MessageStoreRedis;
import com.bsj.travel.mqtt.cluster.MqttServerManageRedis;
import com.bsj.travel.mqtt.listener.MqttConnectStatusListener;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.core.server.MqttServer;
import net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener;
import net.dreamlu.iot.mqtt.core.server.serializer.IMessageSerializer;
import net.dreamlu.iot.mqtt.core.server.store.IMqttMessageStore;
import net.dreamlu.mica.redis.cache.MicaRedisCache;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author yinhao
 * @version 1.0
 * @description mqtt broker 配置
 * @date 2023/11/9
 */
@Slf4j
public class MqttBrokerConfiguration {

    @Bean
    public IMessageSerializer messageSerializer() {
        return new JacksonMessageSerializer();
    }

    @Bean
    public IMqttConnectStatusListener mqttConnectStatusListener(ApplicationContext context, MicaRedisCache redisCache,
                                                                RedisCached redisCached, BaseMqPushUtil baseMqPushUtil) {
        return new MqttConnectStatusListener(context, redisCache, redisCached, baseMqPushUtil);
    }

    @Bean
    public IMqttMessageStore mqttMessageStore(MicaRedisCache redisCache, IMessageSerializer messageSerializer) {
        return new MessageStoreRedis(redisCache, messageSerializer);
    }

//    @Bean
//    public MqttMessageExchangeReceiverRedis mqttMessageUpReceiver(IMessageSerializer messageSerializer, MqttServer mqttServer) {
//        return new MqttMessageExchangeReceiverRedis(messageSerializer, mqttServer);
//    }
//
//    @Bean
//    public MqttMessageDownReceiverRedis mqttMessageDownReceiver(IMessageSerializer messageSerializer, MqttServer mqttServer) {
//        return new MqttMessageDownReceiverRedis(messageSerializer, mqttServer);
//    }
//
//    @Bean
//    public IMqttMessageDispatcher mqttMessageDispatcher(RStreamTemplate streamTemplate, IMessageSerializer messageSerializer) {
//        return new MqttMessageDispatcherRedis(streamTemplate, messageSerializer, Objects.requireNonNull(RedisKeys.REDIS_CHANNEL_EXCHANGE.getKey(), "Redis pub/sub channel is null"));
//    }

    @Bean
    public MqttServerManageRedis mqttServerManage(MicaRedisCache micaRedisCache, MqttServer mqttServer) {
        return new MqttServerManageRedis(micaRedisCache, mqttServer);
    }

}
